

import { Api } from '@/servce'
import COS from 'cos-wx-sdk-v5'

class utils {
    constructor(){
        this.init()
    }
    cos:{}
    credentical:{}
    async init(){//@ts-ignore
        let { result }  = await Api.user.getCredential()
        this.cos = new COS({
          getAuthorization: function (options, callback) {
            callback({
              TmpSecretId: result.credentials.tmpSecretId,
              TmpSecretKey: result.credentials.tmpSecretKey,
              XCosSecurityToken: result.credentials.sessionToken,
              StartTime: result.startTime, // 时间戳，单位秒，如：1580000000，建议返回服务器时间作为签名的开始时间，避免用户浏览器本地时间偏差过大导致签名错误
              ExpiredTime: result.expiredTime, // 时间戳，单位秒，如：1580000900
            })
          }
        });
        this.credentical = result;
    }
    async uploadFile (file:any,imgkey,callback){     
        let _this = this        
        //@ts-ignore
        this.cos.postObject({//@ts-ignore
            Bucket:  _this.credentical.bucketInfo.bucket,//@ts-ignore,
            Region:  _this.credentical.bucketInfo.region,
            Key: imgkey,
            FilePath:  file,
            onProgress: function (info) {
                console.log(JSON.stringify(info));
            }
        }, function (err, data) {
             console.log("[cos.postObject-err]", err || data);
					  callback(data)
        });
    }        
}
export function getTimeFun(){
    let now = new Date();
    let year = now.getFullYear(); 
    let month = ((now.getMonth()+1)<10?"0":"") + (now.getMonth()+1);
    let day = (now.getDate()<10?"0":"")+now.getDate();
    let time=year+'-'+month+"-"+day;
    return time;
}
export default utils